<template>
    <div class="detail">
        <div class="detail-header">
            <van-nav-bar title="商品详情" left-arrow @click-left="onClickLeft">
                <!-- <template #right> <van-icon name="require('@/asset/fx.png')" size="18" /> </template> -->
            </van-nav-bar>
        </div>

        <van-swipe class="my-swipe" indicator-color="white">
            <van-swipe-item>
                <!-- <img :src="`https://images.weserv.nl/?url=${shopping_detail.content.goods_img}`" alt="" /> -->
                <img style="width: 100%; height: 100%" :src="shopping_detail.content.goods_img" alt="" />
            </van-swipe-item>
            <van-swipe-item>2</van-swipe-item>
            <van-swipe-item>3</van-swipe-item>
            <van-swipe-item>4</van-swipe-item>
        </van-swipe>

        <div class="detail-title-box">
            <p class="deatil_title">{{ shopping_detail.content.goods_name }}</p>
            <p class="detail_price">￥{{ shopping_detail.content.goods_price }}<span>￥499</span></p>
        </div>

        <div class="note-box">
            <div class="icons">
                <i></i>
                <p>品牌保障</p>
            </div>
            <div class="icons">
                <i></i>
                <p>支持7天退换货</p>
            </div>
        </div>

        <div class="navs-box">
            <van-tabs title-active-color="#000" @change="getComments" v-model:active="activeName" line-width="100px">
                <van-tab title="晒单评论(0)" name="comments">晒单评论(0)</van-tab>
                <van-tab title="常见问题" name="problem">
                    <ul class="problem-list">
                        <li>
                            <div class="ask"><span class="qq">Q:</span><span>商品都是正品吗？</span></div>
                            <div class="answer"><span class="aa">A:</span><span>快点恢复开放还是开发计划计划扩建后的科技示范的方式发送</span></div>
                        </li>
                        <li>
                            <div class="ask"><span class="qq">Q:</span><span>商品都是正品吗？</span></div>
                            <div class="answer"><span class="aa">A:</span><span>快点恢复开放还是开发计划计划扩建后的科技示范的方式发送</span></div>
                        </li>
                        <li>
                            <div class="ask"><span class="qq">Q:</span><span>商品都是正品吗？</span></div>
                            <div class="answer"><span class="aa">A:</span><span>快点恢复开放还是开发计划计划扩建后的科技示范的方式发送</span></div>
                        </li>
                    </ul>
                    <div class="look">
                        <p>查看更多</p>
                        <i></i>
                    </div>
                </van-tab>
            </van-tabs>
        </div>

        <div class="footer-box">
            <div class="shop">
                <i class="shop-icon"></i>
                <p>品牌店铺</p>
            </div>
            <div class="favorites" @click="addLike(shopping_detail.content.gid)">
                <i :class="islike ? 'favor-like' : 'favor-notlike'"></i>
                <p>{{ islike ? "已收藏" : "未收藏" }}</p>
            </div>
            <div class="addcart">
                <button @click="showPopup">加入购物车</button>
            </div>
        </div>

        <van-popup v-model:show="show" overlay closeable close-icon="close" position="bottom" :style="{ height: 'auto' }">
            <div class="pop-ups">
                <div class="infos">
                    <div class="basic-info">
                        <div class="thumb-img">
                            <img :src="shopping_detail.content.goods_img" alt="" />
                        </div>
                        <div class="text-info">
                            <p class="price">
                                <span class="sale-price">￥{{ shopping_detail.content.goods_price }}</span
                                ><span class="market-price">￥499</span>
                            </p>
                            <p class="choosed-info" v-if="color == '' && size == ''">请选择颜色尺码</p>
                            <p class="choosed-info" v-else>颜色:{{ color }} 尺码:{{ size }}</p>
                        </div>
                    </div>
                    <div class="chose-items">
                        <div class="block-list">
                            <p class="name">颜色</p>
                            <ul>
                                <li :class="color == item ? 'active' : ''" v-for="item in colorlist" @click="getColor(item)">
                                    <p>{{ item }}</p>
                                </li>
                            </ul>
                        </div>
                        <div class="block-list">
                            <p class="name">尺码</p>
                            <ul>
                                <li :class="size == item ? 'active' : ''" v-for="item in sizelist" @click="getSize(item)">
                                    <p>{{ item }}</p>
                                </li>
                                <!-- <li @click="getSize">
                                    <p>L</p>
                                </li>
                                <li @click="getSize">
                                    <p>XL</p>
                                </li>
                                <li @click="getSize">
                                    <p>XXL</p>
                                </li>
                                <li @click="getSize">
                                    <p>XXXL</p>
                                </li> -->
                            </ul>
                        </div>
                        <div class="nums">
                            <span>数量</span>
                            <van-stepper v-model="nums" input-width="40px" button-size="32px" />
                        </div>
                    </div>
                </div>
            </div>
            <button @click="addCart">加入购物车</button>
        </van-popup>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { getShoppingDetail, addShoppingCart, addLikes, notLikes, getLikes } from "../../api/index";
import { useUserInfo } from "../../store/user";
import { useRoute, useRouter } from "vue-router";
import { showToast } from "vant";
let router = useRouter();
let route = useRoute();

const store = useUserInfo();

let colorlist = ["白色", "黑色", "灰色", "天蓝色"];
let sizelist = ["M", "L", "XL", "XXL", "XXXL"];

let islike = ref(false);

//评论
let comments = reactive({
    list: [],
});

//收藏和取消收藏
const addLike = (gid: any) => {
    if (islike.value) {
        notLikes({ user_id: store.$state.user_id, like_gid: gid }).then((res: any) => {
            console.log(res);
            if (res.code == 200) {
                islike.value = false;
            }
        });
    } else {
        addLikes({ user_id: store.$state.user_id, like_gid: gid }).then((res: any) => {
            if (res.code == 200) {
                islike.value = true;
            }
        });
    }
};

//检查是否已收藏
getLikes().then((res: any) => {
    let likeArr = res.content;
    let gid = route.query.gid;
    likeArr.forEach((item: any) => {
        if (item.like_gid == gid) {
            islike.value = true;
        }
    });
});

const getComments = () => {
    showToast("暂时无评论");
    // if (comments.list.length == 0) {
    //     activeName.value = "problem";
    //     console.log(activeName.value);
    // }
};

//切换导航
let activeName = ref("problem");
//是否显示弹窗
let show = ref(false);
//记录购买数量
let nums = ref("1");

//储存商品详情
let shopping_detail = reactive({
    content: {},
});
//记录颜色
let color = ref("");
//记录尺码
let size = ref("");

//加入购物车
const addCart = () => {
    if (color.value == "") {
        showToast("请选择颜色");
        return;
    }
    if (size.value == "") {
        showToast("请选择尺码");
        return;
    }
    let data = {
        gid: shopping_detail.content["gid"],
        goods_img: shopping_detail.content["goods_img"],
        goods_name: shopping_detail.content["goods_name"],
        goods_price: shopping_detail.content["goods_price"],
        goods_color: color.value,
        goods_size: size.value,
        goods_num: nums.value,
    };
    addShoppingCart(data).then((res: any) => {
        console.log(res);
        if (res.code == -1) {
            showToast(res.msg);
            localStorage.removeItem("auto_token");
            localStorage.removeItem("user_info");
            store.getInfo({});
            router.push({
                path: "/login",
            });
        }
        if (res.code == 200) {
            showToast(res.msg);
        }
    });
};

//返回上一级
const onClickLeft = () => history.back();
//点击显示弹窗
const showPopup = () => {
    show.value = true;
};

const getColor = (c: string) => {
    // console.log(c);
    color.value = c;
};
const getSize = (s: string) => {
    // console.log(s);
    size.value = s;
};

//获取商品详情
let data = {
    gid: route.query.gid,
};
getShoppingDetail(data).then((res: any) => {
    // console.log(res.content[0]);
    shopping_detail.content = res.content[0];
    // console.log(shopping_detail.content);
});
</script>

<style lang="scss" scoped>
.detail {
    position: relative;
    background: #f2f2f2;
    padding: 46px 0 60px;
    // height: 1000px;
}

//弹出层
.van-popup {
    .pop-ups {
        padding: 0 15px;

        .basic-info {
            position: relative;
            min-height: 120px;
            display: flex;
            border-bottom: 1px solid #f0f0f0;

            .thumb-img {
                border: 2px solid #fff;
                position: absolute;
                top: -20px;
                left: 0;
                width: 94px;
                height: 126px;
                img {
                    width: 100%;
                    height: 100%;
                }
            }
            .text-info {
                margin-left: 95px;
                padding: 5px 10px 0;
            }
            .price {
                .sale-price {
                    padding-right: 10px;
                    color: #c7273a;
                }
                .market-price {
                    color: #999;
                    text-decoration: line-through;
                }
            }
            .choosed-info {
                padding-top: 5px;
                color: #313131;
                font-size: 13px;
            }
        }

        .block-list {
            // height: auto;
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            // display: flex;
            // align-items: center;

            .name {
                font-size: 14px;
                color: #313131;
                padding-bottom: 10px;
            }

            ul {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            li {
                height: 28px;
                box-sizing: border-box;
                min-width: 50px;
                margin-right: 10px;
                border: 1px solid #313131;
                border-radius: 5px;
                padding: 5px 10px;
                text-align: center;
                font-size: 14px;
            }
            .active {
                background: #c7273a;
                border-color: #c7273a;
                color: #fff;
            }
        }

        .nums {
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            span {
                padding-right: 20px;
            }
        }
    }
    button {
        padding: 8px 0;
        width: 100%;
        text-align: center;
        background: #c7273a;
        font-size: 16px;
        color: #fff;
    }
}

.detail-header {
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
}
.footer-box {
    z-index: 1;
    border-top: 1px solid #f2f2f2;
    position: fixed;
    bottom: -1px;
    left: 0;
    background: #fff;
    padding: 10px 10px;
    width: 100%;
    display: flex;
    align-items: center;
    i {
        margin: 0 auto;
        display: block;
        width: 20px;
        height: 20px;
        background-repeat: no-repeat;
        background-size: 100%;
    }
    p {
        text-align: center;
        font-size: 12px;
        color: #2c2c2c;
    }
    .shop-icon {
        background-image: url(@/assets/dp.png);
    }
    .favor-like {
        background-image: url(@/assets/sc2.png);
    }
    .favor-notlike {
        background-image: url(@/assets/sc.png);
    }
    .shop {
        width: 30%;
    }
    .favorites {
        width: 30%;
    }
    .addcart {
        flex-grow: 1;
        text-align: center;
        button {
            border-color: #c7273a;
            background: #c7273a;
            color: #fff;
            font-size: 16px;
            border-radius: 5px;
            padding: 7px 20px;
        }
    }
}
:deep(.van-popup) {
    overflow-y: initial;
}
:deep(.van-nav-bar) {
    background: #353535;
}
:deep(.van-nav-bar .van-icon) {
    color: #fff;
}
:deep(.van-nav-bar__title) {
    color: #fff;
    font-weight: normal;
}
:deep(.van-tab--active) {
    color: #000;
    font-weight: normal;
}
:deep(.van-tab) {
    color: #999;
}
:deep(.van-tabs--line .van-tabs__wrap) {
    border-bottom: 1px solid #f0f0f0;
}
:deep(.van-tabs__line) {
    background: #000;
    border-radius: 0;
}
:deep(.van-tabs__content) {
    padding: 0 10px;
}
.my-swipe .van-swipe-item {
    height: 400px;
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    background-color: #39a9ed;
}
.detail-title-box {
    background: #fff;
    padding: 10px 10px;
    .deatil_title {
        font-size: 16px;
        color: #313131;
        height: 40px;
    }
    .detail_price {
        padding-top: 5px;
        color: #c7273a;
        span {
            font-size: 14px;
            color: #999;
            margin-left: 5px;
            text-decoration: line-through;
        }
    }
}
.note-box {
    background: #fff;
    width: 100%;
    padding: 10px 10px;
    margin: 10px 0;
    display: flex;
    .icons {
        padding-right: 13px;
        display: flex;
        align-items: center;
        i {
            display: block;
            width: 12px;
            height: 12px;
            background-image: url(@/assets/bz.png);
            background-repeat: no-repeat;
            background-size: 100%;
        }
        p {
            padding-left: 5px;
            font-size: 12px;
            color: #515151;
        }
    }
}
.navs-box {
    // padding-bottom: 70px;
    width: 100%;
    background: #fff;

    .problem-list {
        li {
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            font-size: 14px;
            color: #313131;
        }
        .ask {
            margin-bottom: 5px;
            display: flex;
            .qq {
                padding-right: 5px;
                color: orangered;
            }
        }
        .answer {
            display: flex;
            .aa {
                padding-right: 5px;
                color: orangered;
            }
            span {
                color: #999;
                font-size: 13px;
            }
        }
    }

    .look {
        padding: 10px 5px;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;

        p {
            font-size: 14px;
            color: #999;
        }
        i {
            display: block;
            width: 10px;
            height: 10px;
            border: 1px solid #999;
            transform: rotate(45deg);
            border-left-color: transparent;
            border-bottom-color: transparent;
        }
    }
}
</style>
